﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WhiteBoard
{
    public partial class Glass : Form
    {
        //定义和窗口移动改变大小相关变量
        bool IsMoving = false;
        Point pthisLastCoordinate = new Point(0, 0);
        Point pCursorOffset = new Point(0, 0);
        Point pCursorLastCoordinate = new Point(0, 0);

        bool IsResize = false;
        System.Drawing.Size oldSize = new System.Drawing.Size(0, 0);

        public Glass()
        {
            InitializeComponent();
        }

        private void Glass_MouseDown(object sender, MouseEventArgs e)
        {

            if (e.Button == MouseButtons.Left)
            {
                if (this.Cursor == Cursors.SizeAll)
                {
                    IsResize = false;
                    IsMoving = true;
                    pthisLastCoordinate.X = this.Left;
                    pthisLastCoordinate.Y = this.Top;
                    pCursorLastCoordinate.X = Cursor.Position.X;
                    pCursorLastCoordinate.Y = Cursor.Position.Y;
                }
                else
                {
                    IsMoving = false;
                    IsResize = true;
                    pthisLastCoordinate.X = this.Left;
                    pthisLastCoordinate.Y = this.Top;
                    pCursorLastCoordinate.X = Cursor.Position.X;
                    pCursorLastCoordinate.Y = Cursor.Position.Y;
                    oldSize = this.Size;
                }
            }
        }

        private void Glass_MouseMove(object sender, MouseEventArgs e)
        {
            if (!IsResize)
            {
                if ((Cursor.Position.X <= this.Left + 8 && Cursor.Position.X >= this.Left - 8 && Cursor.Position.Y >= this.Top + this.Height - 8 && Cursor.Position.Y <= this.Top + this.Height + 8) ||
                    (Cursor.Position.X <= this.Left + this.Width + 8 && Cursor.Position.X >= this.Left + this.Width - 8 && Cursor.Position.Y >= this.Top - 8 && Cursor.Position.Y <= this.Top + 8))
                {
                    //MessageBox.Show("ok");
                    this.Cursor = Cursors.SizeNESW;
                }
                else if ((Cursor.Position.X <= this.Left + 8 && Cursor.Position.X >= this.Left - 8 && Cursor.Position.Y >= this.Top - 8 && Cursor.Position.Y <= this.Top + 8) ||
                    (Cursor.Position.X <= this.Left + this.Width + 8 && Cursor.Position.X >= this.Left + this.Width - 8 && Cursor.Position.Y >= this.Top + this.Height - 8 && Cursor.Position.Y <= this.Top + this.Height + 8))
                {
                    this.Cursor = Cursors.SizeNWSE;
                }
                else if ((Cursor.Position.X >= this.Left + 8 && Cursor.Position.X <= this.Left + this.Width - 8 && Cursor.Position.Y >= this.Top - 5 && Cursor.Position.Y <= this.Top + 5) || (Cursor.Position.X >= this.Left + 8 && Cursor.Position.X <= this.Left + this.Width - 8 && Cursor.Position.Y >= this.Top + this.Height - 5 && Cursor.Position.Y <= this.Top + this.Height + 5))
                {
                    this.Cursor = Cursors.SizeNS;
                }
                else if ((Cursor.Position.Y >= this.Top + 8 && Cursor.Position.Y <= this.Top + this.Height - 8 && Cursor.Position.X >= this.Left - 5 && Cursor.Position.X <= this.Left + 5) || (Cursor.Position.Y >= this.Top + 8 && Cursor.Position.Y <= this.Top + this.Height - 8 && Cursor.Position.X >= this.Left + this.Width - 5 && Cursor.Position.X <= this.Left + this.Width + 5))
                {
                    this.Cursor = Cursors.SizeWE;
                }
                else this.Cursor = Cursors.SizeAll;
            }

            if (e.Button == MouseButtons.Left)
            {

                Point pCursor = new Point(Cursor.Position.X, Cursor.Position.Y);
                pCursorOffset.X = pCursor.X - pCursorLastCoordinate.X;
                pCursorOffset.Y = pCursor.Y - pCursorLastCoordinate.Y;
                if (this.IsMoving)
                {
                    //    Point pCursor = new Point(Cursor.Position.X, Cursor.Position.Y);
                    //    pCursorOffset.X = pCursor.X - pCursorLastCoordinate.X;
                    //    pCursorOffset.Y = pCursor.Y - pCursorLastCoordinate.Y;
                    this.Left = pthisLastCoordinate.X + pCursorOffset.X;
                    this.Top = pthisLastCoordinate.Y + pCursorOffset.Y;
                }
                else if (this.IsResize)
                {
                    if (this.Cursor == Cursors.SizeNWSE)
                    {

                        if (Cursor.Position.X <= this.Left + this.Width + 8 && Cursor.Position.X >= this.Left + this.Width - 8 && Cursor.Position.Y >= this.Top + this.Height - 8 && Cursor.Position.Y <= this.Top + this.Height + 8)
                        {
                            if (oldSize.Width + pCursorOffset.X >= 50 && oldSize.Height + pCursorOffset.Y >= 50)
                            {
                                this.Width = oldSize.Width + pCursorOffset.X;
                                this.Height = oldSize.Height + pCursorOffset.Y;

                            }
                        }
                        else if (Cursor.Position.X <= this.Left + 8 && Cursor.Position.X >= this.Left - 8 && Cursor.Position.Y >= this.Top - 8 && Cursor.Position.Y <= this.Top + 8)
                        {
                            if (oldSize.Width - pCursorOffset.X >= 50 && oldSize.Height - pCursorOffset.Y >= 50)
                            {
                                this.Left = pthisLastCoordinate.X + pCursorOffset.X;
                                this.Top = pthisLastCoordinate.Y + pCursorOffset.Y;
                                this.Width = oldSize.Width - pCursorOffset.X;
                                this.Height = oldSize.Height - pCursorOffset.Y;
                            }
                        }
                    }
                    else if (this.Cursor == Cursors.SizeWE)
                    {
                        if (Cursor.Position.Y >= this.Top + 8 && Cursor.Position.Y <= this.Top + this.Height - 8 && Cursor.Position.X >= this.Left + this.Width - 5 && Cursor.Position.X <= this.Left + this.Width + 5)
                        {
                            if (oldSize.Width + pCursorOffset.X >= 50)
                            {
                                this.Width = oldSize.Width + pCursorOffset.X;
                            }
                        }
                        else if (Cursor.Position.Y >= this.Top + 8 && Cursor.Position.Y <= this.Top + this.Height - 8 && Cursor.Position.X >= this.Left - 5 && Cursor.Position.X <= this.Left + 5)
                        {
                            if (oldSize.Width - pCursorOffset.X >= 50)
                            {
                                this.Left = pthisLastCoordinate.X + pCursorOffset.X;
                                this.Width = oldSize.Width - pCursorOffset.X;
                            }
                        }
                    }
                    else if (this.Cursor == Cursors.SizeNS)
                    {
                        if (Cursor.Position.X >= this.Left + 8 && Cursor.Position.X <= this.Left + this.Width - 8 && Cursor.Position.Y >= this.Top - 5 && Cursor.Position.Y <= this.Top + 5)
                        {
                            if (oldSize.Height - pCursorOffset.Y >= 50)
                            {
                                this.Top = pthisLastCoordinate.Y + pCursorOffset.Y;
                                this.Height = oldSize.Height - pCursorOffset.Y;
                            }
                        }
                        else if (Cursor.Position.X >= this.Left + 8 && Cursor.Position.X <= this.Left + this.Width - 8 && Cursor.Position.Y >= this.Top + this.Height - 5 && Cursor.Position.Y <= this.Top + this.Height + 5)
                        {
                            if (oldSize.Height + pCursorOffset.Y >= 50)
                            {
                                this.Height = oldSize.Height + pCursorOffset.Y;
                            }
                        }
                    }
                    else if (this.Cursor == Cursors.SizeNESW)
                    {
                        if (Cursor.Position.X <= this.Left + 8 && Cursor.Position.X >= this.Left - 8 && Cursor.Position.Y >= this.Top + this.Height - 8 && Cursor.Position.Y <= this.Top + this.Height + 8)
                        {
                            if (oldSize.Width - pCursorOffset.X >= 50 && oldSize.Height + pCursorOffset.Y >= 50)
                            {
                                this.Left = pthisLastCoordinate.X + pCursorOffset.X;
                                this.Width = oldSize.Width - pCursorOffset.X;
                                this.Height = oldSize.Height + pCursorOffset.Y;
                            }
                        }
                        else if (Cursor.Position.X <= this.Left + this.Width + 8 && Cursor.Position.X >= this.Left + this.Width - 8 && Cursor.Position.Y >= this.Top - 8 && Cursor.Position.Y <= this.Top + 8)
                        {
                            if (oldSize.Width + pCursorOffset.X >= 50 && oldSize.Height - pCursorOffset.Y >= 50)
                            {
                                this.Top = pthisLastCoordinate.Y + pCursorOffset.Y;
                                this.Width = oldSize.Width + pCursorOffset.X;
                                this.Height = oldSize.Height - pCursorOffset.Y;
                            }
                        }
                    }



                }
            }
        }

        private void Glass_MouseUp(object sender, MouseEventArgs e)
        {
            if (this.IsMoving)
            {
                if (pCursorOffset.X == 0 && pCursorOffset.Y == 0)
                {
                    return;
                }
                if ((pthisLastCoordinate.X + pCursorOffset.X + this.Width) > 0)
                {
                    this.Left = pthisLastCoordinate.X + pCursorOffset.X;
                }
                else
                {
                    this.Left = 0;
                }
                if ((pthisLastCoordinate.Y + pCursorOffset.Y + this.Height) > 0)
                {
                    this.Top = pthisLastCoordinate.Y + pCursorOffset.Y;
                }
                else
                {
                    this.Top = 0;
                }

            }
            IsResize = false;
            pCursorOffset.X = 0;
            pCursorOffset.Y = 0;
        }

        private void Glass_Move(object sender, EventArgs e)
        {
            SpotLight f = (SpotLight)this.Owner;
            f.pictureBox.Location = this.Location;
        }

        private void Glass_Resize(object sender, EventArgs e)
        {
            SpotLight f = (SpotLight)this.Owner;
            f.pictureBox.Size = this.Size;
        }
    }
}
